package mark.tom.friend;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;

import static mark.tom.friend.C.AUTHORITY;
import static mark.tom.friend.C.KEY_EXTRA;
import static mark.tom.friend.C.KEY_SERVER;
import static mark.tom.friend.C.KEY_SWITCH;
import static mark.tom.friend.C.PARAM;

final class ConfigContentProvider {

    public static ConfigContent getContent(ContentResolver contentResolver, String packageName) {
        ConfigContent setting = new ConfigContent();
        Uri PACKAGE_URI = Uri.parse("content://" + AUTHORITY + "/" + PARAM);
        Cursor cursor = contentResolver.query(PACKAGE_URI, null, null,
                new String[]{packageName}, null);
        if (cursor != null) {
            int resultCount = cursor.getCount();
            if (1 == resultCount && cursor.moveToFirst()) {
                setting.setServerConfig(cursor.getString(cursor
                        .getColumnIndex(KEY_SERVER)));
                setting.setExtraConfig(cursor.getString(cursor
                        .getColumnIndex(KEY_EXTRA)));
                setting.setDebugAble(1 == (cursor.getInt(cursor
                        .getColumnIndex(KEY_SWITCH))));
            } else {
                setting.setDebugAble(false);
            }
            cursor.close();
        } else {
            setting.setDebugAble(false);
        }
        return setting;
    }
}
